package user

import (
	"cs0512/fmr/internal/apiserver/domain"
	"cs0512/fmr/internal/apiserver/store"
	"cs0512/fmr/internal/pkg/response"
	"cs0512/fmr/pkg/model"

	"github.com/gin-gonic/gin"
	"github.com/marmotedu/log"
)

type UserController struct {
	srv domain.Service
}

func NewUserController(store store.Factory) *UserController {
	return &UserController{
		srv: domain.NewService(store),
	}
}

type CrateUserRequest struct {
	Username string `json:"username" form:"username" binding:"required"`
	Password string `json:"password" form:"password" binding:"required"`
	Name     string `json:"name" binding:"required"`
	Mobile   string `json:"mobile" form:"mobile" binding:"required"`
	OrgId    int    `json:"orgId" form:"orgId" binding:"required"`
	Role     int    `json:"role" form:"role" binding:"required"`
	Area     string `json:"area" form:"area"`
}

// 添加用户
func (uc *UserController) Create(c *gin.Context) {
	var ur CrateUserRequest
	if err := c.ShouldBind(&ur); err != nil {
		log.Infof("create user failed, error:%s", err.Error())
		response.Fail(c, 1002, "参数错误")
		return
	}

	u := &model.User{
		Username: ur.Username,
		Password: ur.Password,
		Name:     ur.Name,
		Mobile:   ur.Mobile,
		OrgId:    ur.OrgId,
		Role:     ur.Role,
		Area:     ur.Area,
	}

	if err := uc.srv.User().Create(c, u); err != nil {
		log.Errorf("create user error %v", err)
		response.Fail(c, 1002, "创建用户失败")
		return
	}

	response.OK(c, nil, "")
}

type EditUserRequest struct {
	Uid    int    `json:"uid" form:"uid" binding:"required"`
	Name   string `json:"name" binding:"required"`
	Mobile string `json:"mobile" form:"mobile" binding:"required"`
	OrgId  int    `json:"orgId" form:"orgId" binding:"required"`
	Role   int    `json:"role" form:"role" binding:"required"`
	Area   string `json:"area" form:"area"`
}

// 修改用户信息
func (uc *UserController) Update(c *gin.Context) {
	var ur EditUserRequest
	if err := c.ShouldBind(&ur); err != nil {
		log.Infof("update user failed, error:%s", err.Error())
		response.Fail(c, 1003, "参数错误")
		return
	}
	u := &model.User{
		ID:     ur.Uid,
		Name:   ur.Name,
		Mobile: ur.Mobile,
		OrgId:  ur.OrgId,
		Role:   ur.Role,
		Area:   ur.Area,
	}

	if err := uc.srv.User().Update(c, u); err != nil {
		log.Errorf("update user error %v", err)
		response.Fail(c, 1003, "创建用户失败")
		return
	}

	response.OK(c, nil, "")
}

// 修改密码
type ChangePwdRequest struct {
	Uid      int    `json:"uid" binding:"required"`
	Password string `json:"password" binding:"required"`
}

func (uc *UserController) ChangePassword(c *gin.Context) {
	var ur ChangePwdRequest
	if err := c.ShouldBind(&ur); err != nil {
		log.Infof("change password failed, error:%s", err.Error())
		response.Fail(c, 1002, "参数错误")
		return
	}

	if ur.Password == "" {
		response.Fail(c, 1002, "参数错误")
		return
	}

	err := uc.srv.User().ChangePassword(c, ur.Uid, ur.Password)
	if err != nil {
		log.Infof("create user failed, error:%s", err.Error())
		response.Fail(c, 2001, "修改密码失败")
		return
	}
	response.OK(c, nil, "")
}
